<template>
  <!-- 快递员管理页面 -->
  <div class="app-container">
    <!-- 上方筛选区域 -->
    <el-card class="box-card search-card-box">
      <el-form ref="form" label-width="100px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="8" style="padding-left: 30px; padding-right: 30px;">
            <el-form-item label="快递员账号" style="margin-bottom: 0px;">
              <el-input v-model="params.account" placeholder="请输入快递员账号" />
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 30px; padding-right: 30px;">
            <el-form-item label="快递员姓名">
              <el-input v-model="params.name" placeholder="请输入快递员姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 30px; padding-right: 30px;">
            <el-form-item label="快递员手机号">
              <el-input v-model="params.phone" placeholder="请输入快递员手机" />
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 30px; padding-right: 30px;">
            <el-form-item label="所属机构" style="margin-top: 20px;">
              <!-- <el-select v-model="params.agentId" style="width: 100%;" placeholder="请输入所属机构">
                <el-option label="区域一" value="shanghai" />
                <el-option label="区域二" value="beijing" />
              </el-select> -->
              <treeselect v-model="params.agentId" :options="options" :normalizer="normalize" placeholder="请选择所属机构" />
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 30px; padding-right: 30px; margin-top: 20px; ">
            <el-button type="primary" @click="searchFn">搜索</el-button>
            <el-button @click="resetFn">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 下方表格区域 -->
    <el-card class="box-card table-card-box" style="margin-top: 20px;">
      <!-- 表格区域 -->
      <el-table
        v-loading="isLoading"
        :data="tableData"
        stripe
        style="width: 100%"
      >
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="120"
        />
        <el-table-column
          prop="account"
          label="快递员账号"
          width="120"
        />
        <el-table-column
          prop="name"
          label="快递员姓名"
          width="120"
        />
        <el-table-column
          prop="mobile"
          label="快递员手机号"
          width="120"
        />
        <el-table-column
          prop="agency.name"
          label="所属机构"
          width="120"
        />
        <el-table-column
          prop="workStatus"
          label="工作状态"
          width="300"
        >
          <template v-slot="{row}">
            <span v-if="row.workStatus" class="tableColumn-status">上班</span>
            <span v-else class="tableColumn-status stop-use">休息</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100"
        >
          <template v-slot="{row}">
            <el-button type="text" size="small" @click="handleClick(row.userId)">作业范围分配</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row style="height: 60px; margin-top: 20px;" type="flex" justify="center">
        <el-pagination
          :current-page="params.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changePageSize"
          @current-change="changePage"
        />
      </el-row>
    </el-card>
  </div>
</template>
<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { getDepartTree } from '@/api/base'
import { getExressApi } from '@/api/courier'
export default {
  components: { Treeselect },
  data() {
    return {
      isLoading: true,
      // 分页和搜索的相关数据
      params: {
        page: 1, // 当前页
        pageSize: 10, // 每页的条目数
        account: '',
        name: '',
        phone: '',
        agentId: null
      },
      total: 0, // 总数
      tableData: [],
      // define options
      options: []
    }
  },
  created() {
    this.initTable()
    this.getTree()
  },
  methods: {
    // 请求数据 渲染功能
    async  initTable() {
      this.isLoading = true
      const res = await getExressApi(this.params)
      // console.log(res)
      this.tableData = res.items
      this.total = Number(res.counts)
      // console.log(this.tableData)
      this.isLoading = false
    },
    // 搜索功能
    searchFn() {
      // console.log('submit!')
      this.initTable()
    },
    // 重置功能
    resetFn() {
      // 清空表单
      this.$refs.form.$el.reset()
      // 复原数据
      this.params = {
        page: 1,
        pageSize: 10,
        account: '',
        name: '',
        phone: '',
        agentId: null
      }
      // 发送渲染请求
      this.initTable()
    },
    // 改变每页的条目数量
    changePageSize(val) {
      // console.log(`每页 ${val} 条`)
      this.params.pageSize = val
      this.initTable()
    },
    // 改变当前页码
    changePage(val) {
      // console.log(`当前页: ${val}`)
      this.params.page = val
      this.initTable()
    },
    // 自定义键名
    normalize(node) {
      return {
        id: node.id, // 使用自定义键名 label
        label: node.name,
        children: node.children
      }
    },
    // 得到所属机构的树形结构
    async getTree() {
      const res = await getDepartTree()
      // console.log(JSON.parse(res))
      this.options = JSON.parse(res)
      // console.log(this.options)
    },
    handleClick(val) {
      console.log(val)
      this.$router.push(`mapCount/${val}`)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-card{
  overflow: visible;
}
  .app-container {
  .search-card-box{
    .el-col{
     ::v-deep .el-form-item__label{
        color: #20232a;
        font-weight: 400;
        text-align: right;
        padding-left: 10px;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        white-space: nowrap;
      }
    }

    .el-input{
      position: relative;
      font-size: 14px;
      display: inline-block;
      width: 100%;
    }
    .el-form-item{
      margin-bottom: 0;

    }
    .el-button{
      width: 90px;
      height: 40px;
      padding: 10px 20px!important;
      border-radius: 5px!important;
      font-weight: 400;
    }
  }
}
.tableColumn-status:before {
    width: 6px;
    height: 6px;
    background: #1dc779;
    border-radius: 50%;
    content: "";
    display: inline-block;
    margin-right: 6px;
    }
    .stop-use:before {
    background-color: #e15536;
}

</style>
